package com.linzm.leetcode.primary.exercises1_20230114;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author zimingl
 * @Date 2023/1/15 1:02
 * @Description: 重新分配字符使所有字符串都相等
 * 做法：累加各个字符 除于 数组长度
 */
public class Demo9_1897 {
    /**
     * 输入：words = ["abc","aabc","bc"]
     * 输出：true
     * 解释：将 words[1] 中的第一个 'a' 移动到 words[2] 的最前面。
     * 使 words[1] = "abc" 且 words[2] = "abc" 。
     * 所有字符串都等于 "abc" ，所以返回 true 。
     */
    public static void main(String[] args) {
        Demo9_1897 demo9_1897 = new Demo9_1897();
        String[] words = new String[] {"caaaaa","aaaaaaaaa","a","bbb","bbbbbbbbb","bbb","cc","cccccccccccc","ccccccc","ccccccc","cc","cccc","c","cccccccc","c"};
//        String[] words = new String[] {"abc","aabc","bc"};
        boolean flag = demo9_1897.makeEqual(words);
        System.out.println(flag);
    }

    public boolean makeEqual(String[] words) {
        if (words.length == 0) {
            return false;
        }
        if (words.length == 1) {
            return true;
        }
        Map<Character, Integer> map = new HashMap<>();
        for (String word : words) {
            for (Character c : word.toCharArray()) {
                int count = map.getOrDefault(c, 0) + 1;
                map.put(c, count);
            }
        }
        for (int num : map.values()) {
            if (num % words.length != 0) {
                return false;
            }
        }
        return true;
    }
}
